package com.leetcode.queue;

/**
 * dfs的解法
 */
public class Leetcode200_2 {

    private void dfs(char[][]grid, int r, int c) {
        int rows = grid.length;
        int cols = grid[0].length;

        if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] == '0') {
            return;
        }

        grid[r][c] = '0';
        dfs(grid,r-1,c);
        dfs(grid,r+1, c);
        dfs(grid, r, c-1);
        dfs(grid, r, c+1);

    }

    public int numIslands(char[][] grid) {
        int rows = grid.length;
        int cols = grid[0].length;
        int islands = 0;
        for (int i =0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == '1') {
                    islands++;
                    dfs(grid, i, j);
                }
            }
        }
        return islands;
    }

}
